package com.cjh.config;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjh.pojo.Admin;
import com.cjh.pojo.User;
import com.cjh.service.AdminService;
import com.cjh.service.UserService;
import com.cjh.utils.Md5SaltEncode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Objects;


/**
 * @author cjh
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    private final Logger logger = LoggerFactory.getLogger(UserRealm.class);


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("=======执行了=》授权doGetAuthorizationInfo======");
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//
//        // 拿到当前登录的对象User
//        Subject subject = SecurityUtils.getSubject();
//        // currentUser=下方 密码认证传入的user
//        User currentUser = (User) subject.getPrincipal();
//        System.out.println("currentUser:"+currentUser);
//        return info;
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        logger.info("=======执行了=》认证doGetAuthorizationInfo======");

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        // userToken 封装控制器传进去的用户信息
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        //将host参数作为传参使用
        String status = userToken.getHost();

        User user=null;
        Admin admin=null;
        // 去数据库验证
        if(Objects.equals(status,"user")){
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.eq("user_name",userToken.getUsername());
            user = userService.getOne(userQueryWrapper);
        }else{
            QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
            adminQueryWrapper.eq("user_name",userToken.getUsername());
            admin = adminService.getOne(adminQueryWrapper);
        }

        // 用户名不存在 抛出UnknownAccountException
        if (Objects.isNull(user)&&Objects.isNull(admin)){
            return null;
        }else{
            //根据身份将loginObj转为对应的实体类
            if(Objects.equals(status,"user")){
                session.setAttribute("user",user);
                logger.info("session:"+session.getAttribute("user"));
                System.out.println("login方法执行后===");

                // 密码认证，shiro自动处理
                return new SimpleAuthenticationInfo(user, user.getPassWord(), "");
            }else if (Objects.equals(status,"admin")){
                session.setAttribute("admin",admin);
                logger.info("session:"+session.getAttribute("admin"));
                System.out.println("login方法执行后===");
                // 密码认证，shiro自动处理
                return new SimpleAuthenticationInfo(admin, admin.getPassWord(), "");
            }
            return null;
        }

    }
}
